package cn.guozeyiblog.blog.utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import android.annotation.SuppressLint;

public class NetTools {
	private static HttpURLConnection connection;
	/**
	 * 做HTTP Request 
	 * @return ArrayList<Content>
	 */
  @SuppressLint("NewApi")
public static ArrayList<Content> DoHttp(String path){
	  String result = "";
	 try {
		URL url = new URL(path);
        connection  = (HttpURLConnection) url.openConnection();
        connection.connect();
        connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),"gbk"));
        String line="";
        while ((line = reader.readLine())!= null) {
			result+=line+"\n";
		}
        reader.close();
        connection.disconnect();
	 } catch (Exception e) {
		e.printStackTrace();
	}
	   //解析XML
	// System.out.println(">>>>>>>>>>>>>>>>>>>>>>>\n"+result);
	 SAXParserFactory factory =  SAXParserFactory.newInstance();
	 SAXXmlContentHandler handler= new SAXXmlContentHandler();
	 try {
		SAXParser parser = factory.newSAXParser();
		
		parser.parse(new ByteArrayInputStream(result.getBytes()), handler);
	} catch (ParserConfigurationException | SAXException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	// Log.v("APP>", ">>>>>>>>>>>>>>>"+handler.getContents().size());
	  return handler.getContents();
  }
}
